﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Protal.Interfaces;
using Microhard.Core.DBUtility;
using Microhard.Protal.DataAccess;
using Microhard.Protal.DTO;
using Microhard.Core;
using Microhard.Protal.Resource;

namespace Microhard.Protal.Impl
{
    public class SiteInfoImpl : ISiteInfo
    {
        #region IRepository<SiteInfoModel> Members

        public Core.DBUtility.ExecutedResult Add(Models.SiteInfoModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardProtalEntities db = new MicrohardProtalEntities();
            Microhard_SiteInfo op = model.ToEntity();
            op.CreationTime = DateTime.Now;

            try
            {
                db.Microhard_SiteInfo.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add("Add", Msg.Impl_Add);
                Log.Error("SiteInfoImpl.Add", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Update(Models.SiteInfoModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardProtalEntities db = new MicrohardProtalEntities();
            Microhard_SiteInfo entity = db.Microhard_SiteInfo.SingleOrDefault(c => c.DataID == model.DataID);

            if (entity != null)
            {
                entity.Key = model.Key;
                entity.Value = model.Value;
                entity.CreationTime = DateTime.Now;
                entity.LinkValue = model.LinkValue;
                entity.InfoType = model.InfoType;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                rtn.ErrorMsg.Add("Update", Msg.Impl_Update);
                Log.Error("SiteInfo_Delete.Update", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            MicrohardProtalEntities db = new MicrohardProtalEntities();
            var toDeltes = from b in db.Microhard_SiteInfo
                           where ids.Contains(b.DataID)
                           select b;
            foreach (var d in toDeltes)
            {
                db.Microhard_SiteInfo.DeleteObject(d);
            }
            if (result.ErrorMsg.Count == 0)
            {
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    result.ErrorMsg.Add("Delete", Msg.Impl_Delete);
                    Log.Error("SiteInfo_Delete.Delete", ex);
                }
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Delete(Models.SiteInfoModel model)
        {
            throw new NotImplementedException();
        }

        public Models.SiteInfoModel GetModelByID(Guid id)
        {
            if (id == Guid.Empty)
                return null;

            MicrohardProtalEntities db = new MicrohardProtalEntities();
            var rtn = db.Microhard_SiteInfo.SingleOrDefault(p => p.DataID == id);

            if (rtn != null)
                return rtn.ToModel();

            return null;
        }

        public Models.SiteInfoModel GetModelByModel(Models.SiteInfoModel model)
        {
            MicrohardProtalEntities db = new MicrohardProtalEntities();
            var entity = db.Microhard_SiteInfo.SingleOrDefault(k => k.Key == model.Key);
            if (entity != null)
                return entity.ToModel();
            return new Models.SiteInfoModel();
        }

        public List<Models.SiteInfoModel> Query(Models.SiteInfoModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            MicrohardProtalEntities db = new MicrohardProtalEntities();
            IEnumerable<Microhard_SiteInfo> entities = from b in db.Microhard_SiteInfo
                                                       select b;
            totalCount = entities.Count();
            var pagedEntities = entities.OrderByDescending(p => p.CreationTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pagedEntities.Select(p => p.ToModel()).ToList();
        }

        public List<Models.SiteInfoModel> Query(Models.SiteInfoModel Model)
        {
            MicrohardProtalEntities db = new MicrohardProtalEntities();
            if (Model.CompanyID != Guid.Empty)
            {
                return db.Microhard_SiteInfo.Where(s => s.CompanyID == Model.CompanyID).ToList().Select(s => s.ToModel()).ToList();
            }
            return db.Microhard_SiteInfo.ToList().Select(s => s.ToModel()).ToList();
        }

        public Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }

        #endregion

        public bool IsDuplicateKey(Models.SiteInfoModel model)
        {
            MicrohardProtalEntities db = new MicrohardProtalEntities();
            var entity = db.Microhard_SiteInfo.SingleOrDefault(s => s.Key == model.Key);
            if (entity == null)
                return false;
            if (entity.DataID == model.DataID)
                return false;
            return true;
        }
    }
}
